﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpotsHub.RouteService.Data
{
    [Table("Route_Location")]
    public class Location : EntityBase, ITimeStamp
    {
        #region Required

        public int AddressId { get; set; }

        public int ContactorId { get; set; }

        //public int PackageId { get; set; }

        #endregion

        #region Optional

        public int? RoutePlanId { get; set; }

        public int? AssignedDriverId { get; set; }

        //public int? RelatedLocationId { get; set; }

        #endregion

        #region Navigator

        public virtual Address Address { get; set; }

        public virtual Contactor Contactor { get; set; }

        public virtual RoutePlan RoutePlan { get; set; }

        public virtual Driver AssignedDriver { get; set; }

        //public virtual Package Package { get; set; }

        public virtual Shipment Shipment { get; set; }

        #endregion

        #region Other

        public DateTime? LastUpdate { get; set; }

        public int? Rank { get; set; }

        public LocationStatus Status { get; set; }

        #endregion

        #region Helper

        [NotMapped]
        public string AddressString 
        {
            get
            {
                return Address.FormattedAddress;
            }
        }

        [NotMapped]
        public PackageStatus? RelatedPackageStatus
        {
            get
            {
                if (Shipment == null || Shipment.Package == null)
                {
                    return null;
                }
                return Shipment.Package.Status;
            }
        }

        [NotMapped]
        public Location RelatedLocation
        {
            get
            {
                if (Status == LocationStatus.PendingDropOff || Status == LocationStatus.ReadyForDropOff)
                {
                    return Shipment.PickUpLocation;
                }
                else if (Status == LocationStatus.ReadyForPickUp)
                {
                    return Shipment.DeliveryLocation;
                }

                return null;
            }
        }

        #endregion
    }

    public enum LocationStatus : byte
    {
        Unknown,
        ReadyForPickUp,
        PendingDropOff,
        ReadyForDropOff,
        Complete
    }
}
